package com.titar.robot.ui.register;


import android.app.Activity;

import com.titar.robot.AppExecutors;
import com.titar.robot.db.CacheTableDao;
import com.titar.robot.db.RobotDatabase;
import com.titar.robot.entity.LoginResponse;
import com.titar.robot.entity.MemberInfo;
import com.titar.robot.entity.RegistResponse;
import com.titar.robot.entity.VerifyCodeResponse;
import com.titar.robot.http.RetrofitApi;
import com.titar.robot.http.service.CustomerService;
import com.titar.robot.repository.Resource;
import com.titar.robot.utils.ToastUtils;

import androidx.lifecycle.Observer;

public class RegistPresenter implements RegisterContact.Presenter {
    private RegisterContact.View v;
    private RegistRepository registRepository;

    public RegistPresenter() {
        AppExecutors appExecutors = AppExecutors.getInstence();
        CacheTableDao cacheTableDao = RobotDatabase.getDatabase().cacheTableDao();
        CustomerService customerService = RetrofitApi.getRequestInterface();

        registRepository = new RegistRepository(appExecutors, cacheTableDao, customerService);
    }

    @Override
    public void getVerifyCode(String phone) {
        registRepository.getVerifyCode(phone)
                .observeForever(new Observer<Resource<VerifyCodeResponse>>() {
                    @Override
                    public void onChanged(Resource<VerifyCodeResponse> verifyCodeResponseResource) {
                        switch (verifyCodeResponseResource.getStatus()) {
                            case ERROR:
                                if (v != null) {
                                    v.dismissLoadingDialog();
                                    v.showErrorDialog(verifyCodeResponseResource.getMessage(), false);
                                }

                                break;
                            case RESULT_ERROR:
                                if (v != null) {
                                    v.dismissLoadingDialog();
                                    v.showErrorDialog(verifyCodeResponseResource.getMessage(), false);
                                }

                                break;
                            case LOADING:
                                if (v != null) {
                                    v.showLoadingDialog();
                                }
                                break;
                            case SUCCESS:
                                if (v != null) {
                                    v.dismissLoadingDialog();
                                    ToastUtils.showToast("验证码请求成功，请注意查收短信");
                                    v.onGetVerifyCodeSuccess(verifyCodeResponseResource.getData());
                                }
                                break;
                        }
                    }
                });
    }

    @Override
    public void regist(String mPhone, String mPsw, String cardCode) {
        registRepository.regist(mPhone,mPsw,cardCode)
                .observeForever(new Observer<Resource<RegistResponse>>() {
                    @Override
                    public void onChanged(Resource<RegistResponse> registResponseResource) {
                        switch (registResponseResource.getStatus()) {
                            case ERROR:
                                if (v != null) {
                                    v.dismissLoadingDialog();
                                    v.showErrorDialog(registResponseResource.getMessage(), false);
                                }

                                break;
                            case RESULT_ERROR:
                                if (v != null) {
                                    v.dismissLoadingDialog();
                                    v.showErrorDialog(registResponseResource.getMessage(), false);
                                }

                                break;
                            case LOADING:
                                if (v != null) {
                                    v.showLoadingDialog();
                                }
                                break;
                            case SUCCESS:
                                if (v != null) {
                                    v.dismissLoadingDialog();
                                    ToastUtils.showToast("注册成功！");
                                    v.onRegistSucess(registResponseResource.getData());
                                }
                                break;
                        }
                    }
                });
    }

    @Override
    public void login(String phone, String password) {
        registRepository.login(phone,password).observeForever(new Observer<Resource<LoginResponse>>() {
            @Override
            public void onChanged(Resource<LoginResponse> loginResponseResource) {
                switch (loginResponseResource.getStatus()) {
                    case ERROR:
                        if (v != null) {
                            v.dismissLoadingDialog();
                            v.showErrorDialog(loginResponseResource.getMessage(), false);
                        }

                        break;
                    case RESULT_ERROR:
                        if (v != null) {
                            v.dismissLoadingDialog();
                            v.showErrorDialog(loginResponseResource.getMessage(), false);
                        }

                        break;
                    case LOADING:
                        if (v != null) {
                            v.showLoadingDialog();
                        }
                        break;
                    case SUCCESS:
                        if (v != null) {
                            v.dismissLoadingDialog();
                            v.onLoginSuccess(loginResponseResource.getData());
                        }
                        break;
                }
            }
        });
    }

    @Override
    public void getMemberInfo(String token) {
        registRepository.getMemberInfo(token).observeForever(new Observer<Resource<MemberInfo>>() {
            @Override
            public void onChanged(Resource<MemberInfo> memberInfoResource) {
                switch (memberInfoResource.getStatus()) {
                    case ERROR:
                        if (v != null) {
                            v.dismissLoadingDialog();
                            v.showErrorDialog( memberInfoResource.getMessage(), false);
                        }

                        break;
                    case RESULT_ERROR:
                        if (v != null) {
                            v.dismissLoadingDialog();
                            v.showErrorDialog(memberInfoResource.getMessage(), false);
                        }

                        break;
                    case LOADING:
                        if (v != null) {
                            v.showLoadingDialog();
                        }
                        break;
                    case SUCCESS:
                        if (v != null) {
                            v.dismissLoadingDialog();
                            v.onGetMemberInfoSuccess(memberInfoResource.getData());
                        }
                        break;
                }
            }
        });
    }

    @Override
    public RegistPresenter attachView(RegisterContact.View view) {
        this.v = view;
        return this;
    }

    @Override
    public void detachView() {
        v = null;
    }
}
